package com.ncmmall.application.securitys.user;

import com.feijin.commons.securitys.MD5HashUtils;
import com.ncmmall.domain.security.user.QWebUser;
import com.ncmmall.domain.security.user.WebUser;
import com.ncmmall.domain.security.user.WebUserRepository;
import com.ncmmall.querychannel.service.QueryChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

/**
 * @author linxs
 * 用户管理（会员、商家、管理员等用户信息）
 */
@Service
@Transactional
public class UserApplication {

    @Autowired
    private WebUserRepository userRepository;


    @Autowired
    private QueryChannelService queryChannelService;

    /**
     * 删除系统用户
     *
     * @param user
     */
    public void remove(WebUser user) {
        userRepository.delete(user);
    }

    /**
     * 添加用户
     *
     * @param user
     * @return
     */
    public WebUser create(WebUser user) {

        String passwordAsMd5 = MD5HashUtils.asMD5(MD5HashUtils.MD5(user.getPassword()), user.getUsername());
        user.setPassword(passwordAsMd5);
        user.setAccountNonLocked(true);
        user.setAccessToken(MD5HashUtils.md5(passwordAsMd5, user.getUsername()));
        user.setCreateTime(new Timestamp(System.currentTimeMillis()));
        userRepository.save(user);
        return user;
    }

    /**
     * 禁止登录
     */
    public void lock(WebUser user) {
        user.setAccountNonLocked(false);
        userRepository.save(user);
    }

    /**
     * 允许登录
     *
     * @param user
     */
    public void unlock(WebUser user) {
        user.setAccountNonLocked(true);
        userRepository.save(user);
    }

    /**
     * 修改用户信息
     *
     * @param params
     */
    public void modify(WebUser params) {
        WebUser user = queryChannelService.findOne(QWebUser.webUser.id.eq(params.getId()), WebUser.class);
        user.setAvatar(params.getAvatar());
        user.setRealName(params.getRealName());
        user.setNickname(params.getNickname());
        user.setMobile(params.getMobile());
        user.setRole(params.getRole());
        user.setEmail(params.getEmail());
        userRepository.save(user);
    }

    /**
     * 重置密码为 123456
     *
     * @param user
     */
    public void resetDefaultPassword(WebUser user) {
        user.setPassword(MD5HashUtils.asMD5(MD5HashUtils.MD5("123456"), user.getUsername()));
        userRepository.save(user);
    }

    /**
     * 修改密码
     */
    public void modifyPassword(WebUser user, String newPassword) {
        user.setFirstLogin(true);
        user.setPassword(MD5HashUtils.asMD5(MD5HashUtils.MD5(newPassword), user.getUsername()));
        userRepository.save(user);
    }

    /**
     * 批量启用
     *
     * @param members
     */
    public void enables(List<WebUser> members) {
        for (WebUser member : members) {
            unlock(member);
        }
    }

    /**
     * 批量禁用
     *
     * @param members
     */
    public void disables(List<WebUser> members) {
        for (WebUser member : members) {
            lock(member);
        }
    }
}
